var setting = {
		view: {
			dblClickExpand: false
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
			onClick: onClick
		}
	};

var zNodes = [];

function onClick(e, treeId, treeNode) {
	var nodes = $.fn.zTree.getZTreeObj("deptTree").getSelectedNodes();
	var deptName = nodes[0].name;
	var deptId = nodes[0].id;
	$("#deptId").val(deptId);
	$("#deptName").val(deptName);
}

function showMenu() {
	var tree= $.fn.zTree.getZTreeObj("deptTree");
	var nodes = tree.getSelectedNodes();
	if(nodes.length > 0){
		tree.cancelSelectedNode(nodes[0]);
	}
	
	var dpId = $("#deptId").val();
	if(!dpId.isBlank()){
		var n = tree.getNodeByParam("id", dpId, null);
		tree.selectNode(n);
	}
	
	$("#menuContent").css({
		left:"0", width:$("#deptName").closest(".input-group").width(),
		top:"30px"}
	).slideDown("fast");
	$("body").bind("mousedown", onBodyDown);
}

function hideMenu() {
	$("#menuContent").fadeOut("fast");
	$("body").unbind("mousedown", onBodyDown);
}

function onBodyDown(event) {
	if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
		hideMenu();
	}
}

function initDeptTree(){
	$.post("/system/role/findAllDeptJson.do",{},function (data){
		var json = eval(data.result);
		zNodes = json;
		$.fn.zTree.init($("#deptTree"), setting, zNodes);
	});
}

$(function (){
	initRoleList();
	initDeptTree();
	validata();
	btnClick();
});

function initRoleList(){
	$("#role-table").jqGrid({
		 url:'/system/role/list.do',
		 mtype: 'POST',
		 datatype : "json",
		 autowidth:true,
		 multiselect: true,
		 multiboxonly:true,
		 colNames:['ID', '名称','备注','所属部门','部门ID'],
		 colModel:[
			{name:'id',index:'id', width:60, editable:false, edittype:'text',hidden:true},
			{name:'name',index:'name', width:90, editable:true, edittype:'text'},
			{name:'mark',index:'mark', width:90, editable:true, edittype:'text'},
			{name:'dept.name',index:'dept.name', width:90, editable:true, edittype:'text'},
			{name:'dept.id',index:'dept.id', width:60, editable:false, edittype:'text',hidden:true}
		 ],
		loadComplete : function() {
			updatePagerIcons(this);
		},
		rowNum:15,
		pager: "#role-pager",
		viewrecords : true,
		onSelectRow: function(id){
			var rowData = $("#role-table").jqGrid('getRowData',id);
			for(var x in rowData){
				$("#validForm input[name='"+x+"']").val(rowData[x]);
			}
	    }
	});
	
	$("#role-table").jqGrid('navGrid',"#role-pager",
	{
		edit: false,
		editicon : 'icon-pencil blue',
		add: false,
		addicon : 'icon-plus-sign purple',
		del: false,
		delicon : 'icon-trash red',
		search: false,
		searchicon : 'icon-search orange',
		refresh: false,
		refreshicon : 'icon-refresh green',
		view: false,
		viewicon : 'icon-zoom-in grey',
	},{},{},{},{},
	{
		recreateForm: true,
		beforeShowForm: function(e){
			var form = $(e[0]);
			form.closest('.ui-jqdialog').find('.ui-jqdialog-title').wrap('<div class="widget-header" />');
		}
	});
}

function validata(){
	$("#validForm").validate({
	 	submitHandler: function() {
	 		var params = $("#validForm").serialize();
			RM.ajax("/system/role/save.do", params,function (data){
				if(data.code == 0){
					RM.jqgrid.reload($("#role-table"));
					formReset();
				}
				RM.alert(data.message);
			});
		}
	});
}

function formReset(){
	$("#validForm input[name='id']").val("");
	$("#validForm")[0].reset();
}

function btnClick(){
	//新增
	$("#grid_btn_add").click(function (){
		formReset();
	});
	//删除
	$("#del_grid-table").click(function (){
		RM.jqgrid.dels($("#role-table"),"dels.do");
	});
	//角色绑定
	$("#roleBind").click(function (){
		var ids = RM.jqgrid.ids($("#role-table"));
		if(ids.length == 0){
			RM.alert("请选择一条记录进行操作");
			return;
		}else{
			var _this = $(this);
			var _title = _this.attr("data-title");
			_title += "-" + $("#validForm input[name='dept.name']").val() + "-" + $("#validForm input[name='name']").val();
			var _url = _this.attr("data-url");
			var _width = _this.attr("data-width");
			var _height = _this.attr("data-height");
			_url += "?id=" + ids[0];
			RM.open(_title,_url,_width,_height,function (){});
		}
	});
	//权限菜单
	$("#roleMenu").click(function (){
		var ids = RM.jqgrid.ids($("#role-table"));
		if(ids.length == 0){
			RM.alert("请选择一条记录进行操作");
			return;
		}else{
			var _this = $(this);
			var _title = _this.attr("data-title");
			_title += "-" + $("#validForm input[name='dept.name']").val() + "-" + $("#validForm input[name='name']").val();
			var _url = _this.attr("data-url");
			var _width = _this.attr("data-width");
			var _height = _this.attr("data-height");
			_url += "?id=" + ids[0];
			RM.open(_title,_url,_width,_height,function (){});
		}
	});
	//显示部门树
	$("#menuBtn").click(function (){
		showMenu();
	});
	//保存角色
	$("#roleSave").click(function (){
		$("#validForm").submit();
	});
}